//
//  ProgressView.swift
//  EasePods
//
//  Created by 李然 on 2024/5/8.
//

import UIKit
import RxSwift

public class ProgressView: View {
    
    public var progress: Double = 0.0 {
        didSet {
            self.setNeedsDisplay()
        }
    }
    
    public var progressColor: UIColor = .black {
        didSet {
            self.setNeedsDisplay()
        }
    }
    
    public override func makeUI() {
        super.makeUI()
        
        self.layer.masksToBounds = true
    }
    
    public override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        guard let context = UIGraphicsGetCurrentContext() else { return }
        context.saveGState()
        defer { context.restoreGState() }
        
        let radio = self.layer.cornerRadius
        
        let progress = min(max(0.0, self.progress), 1.0)
        
        let path = UIBezierPath.init(roundedRect: CGRect(x: 0, y: 0, width: CGFloat(progress) * rect.width, height: rect.height), byRoundingCorners: .allCorners, cornerRadii: CGSize(width: radio, height: radio))
        context.setFillColor(progressColor.cgColor)
        path.fill()
    }
}

public class RotateGearView: ImageView {
    
    public override func makeUI() {
        super.makeUI()
        
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnimation.toValue = CGFloat.pi * 2
        rotationAnimation.duration = 1
        rotationAnimation.repeatCount = .infinity
        rotationAnimation.isRemovedOnCompletion = false
        rotationAnimation.fillMode = .forwards
        
        self.layer.add(rotationAnimation, forKey: "rotationAnimation")
    }
}
